/*
 * txcontroller.hh
 *
 *  Created on: 2011-1-9
 *      Author: xuhui
 */

#ifndef TXCONTROLLER_HH_
#define TXCONTROLLER_HH_
#include <click/element.hh>
CLICK_DECLS

enum TXState{
	FAILED = 0,
	SUCCESS = 1,
	TXING = 2,
	IDLE = 3
};

class TXController: public Element{
public:
	TXController();
	~TXController();

	const char *class_name() const		{ return "TXController"; }
	const char *port_count() const		{ return "2/1"; }
	const char *processing() const		{ return "lh/l"; }

	void *cast(const char *);

	int configure(Vector<String> &conf, ErrorHandler *);

	void push(int port, Packet *p);
	Packet *pull(int port);

private:

	inline void inc_cw(){
		_logcw<_logcwmax ? _logcw++ : _logcw = _logcwmax;
	}

	inline void reset_cw(){
		_logcw = _logcwmin;
	}

	void set_cw();

	inline void set_state(TXState newstate){
		_state = newstate;
	}

	void drop_buffer();

private:

	TXState _state;
	u_int8_t _logcwmin;
	u_int8_t _logcwmax;
	u_int8_t _logcw;
	int _max_tries;
	int _tries;
	Packet * _p;
	int _drops;

};

CLICK_ENDDECLS

#endif /* TXCONTROLLER_HH_ */
